package com.lingxin.gps.sdk;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class MapsUtils {
	final static DecimalFormat df = new DecimalFormat("#.000");
	
	final static double[] TableY = new double[660 * 450];
	final static double[] TableX = new double[660 * 450];
	static boolean isInit=false;
	private static void init() {
		isInit=true;
		String s = "";
		try {
			File file=null;
			file=org.apache.commons.io.FileUtils.toFile(MapsUtils.class.getResource("/Mars2Wgs.txt"));
			s = org.apache.commons.io.FileUtils.readFileToString(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Pattern pattern = Pattern.compile("(\\d+)");
		Matcher mp = pattern.matcher(s);
		int i = 0;
		while (mp.find()) {
			String temp = mp.group();
			if (i % 2 == 0) {
				TableX[i / 2] = Double.valueOf(temp) / 100000.0;
			} else {
				TableY[(i - 1) / 2] = Double.valueOf(temp) / 100000.0;
			}
			i++;
		}
	}
	/**
	 * 经纬偏移设置
	 * @param lng
	 * @param lat
	 * @return
	 */
	public final static double[] gps(double lng, double lat) {
		int i, j;
		double x1, y1, x2, y2, x3, y3, x4, y4, xtry, ytry, dx, dy;
		double t, u;
		xtry = lng;
		ytry = lat;
		//台湾地区不偏移
		 if ((lng >= 120.09999999999999D) && (lng <= 121.99299999999999D) && (lat >= 21.899999999999999D) && (lat <= 25.399999999999999D)) {
		      return new double[] { lng, lat };
		    }
		if(!isInit){
			init();
		}
			if (xtry < 72 || xtry > 137.9 || ytry < 10 || ytry > 54.9) {

				//System.out.println("错误的参数:"+lng+","+lat);
				
				return new double[]{lng,lat};
			}

			i = (int) ((xtry - 72.0) * 10.0);
			j = (int) ((ytry - 10.0) * 10.0);

			x1 = TableX[GetID(i, j)];
			y1 = TableY[GetID(i, j)];
			x2 = TableX[GetID(i + 1, j)];
			y2 = TableY[GetID(i + 1, j)];
			x3 = TableX[GetID(i + 1, j + 1)];
			y3 = TableY[GetID(i + 1, j + 1)];
			x4 = TableX[GetID(i, j + 1)];
			y4 = TableY[GetID(i, j + 1)];

			t = (xtry - 72.0 - 0.1 * i) * 10.0;
			u = (ytry - 10.0 - 0.1 * j) * 10.0;

			dx = (1.0 - t) * (1.0 - u) * x1 + t * (1.0 - u) * x2 + t * u * x3
					+ (1.0 - t) * u * x4 - xtry;
			dy = (1.0 - t) * (1.0 - u) * y1 + t * (1.0 - u) * y2 + t * u * y3
					+ (1.0 - t) * u * y4 - ytry;

			xtry = xtry + dx;
			ytry= ytry + dy;
		return new double[] { xtry, ytry };
	}

	private  static int GetID(int I, int J) {
		return I + 660 * J;
	}

	/**
	 * 生成车辆图标
	 * @param path 图片路径
	 * @param carid
	 * @param carno
	 * @param msg
	 * @param isAcc
	 * @param isSpeed
	 * @param isAlarm
	 * @param fx 方向
	 * @return
	 */
	public static int genImage(String path,String carid,String carno,String msg,boolean isAcc,boolean isSpeed,boolean isAlarm,int fx){
		int status=0;
		if(isAcc){//正在行驶
			if(isSpeed)
				status=1;
			else
				status=4;
		}else{
				status=5;
		}
		if(isAlarm){
			status=2;
		}
//		ImageUtils.genImage(path + "/car/car"+status+".png", path + "/car/"
//				+ carid + "_"+status+".png", carno,
//				msg, fx);
		return status;
	}
	/**
	 * 计算距离
	 * @param n1
	 * @param e1
	 * @param n2
	 * @param e2
	 * @return
	 */
	public static double Distance(double n1, double e1, double n2, double e2) {
		double jl_jd = 102834.74258026089786013677476285;
		double jl_wd = 111712.69150641055729984301412873;
		double b = Math.abs((e1 - e2) * jl_jd);
		double a = Math.abs((n1 - n2) * jl_wd);
		return Math.sqrt((a * a + b * b));

	}
	/**
	 * 
	 <param name="n1">第一点的纬度坐标</param>latitude1
	 <param name="e1">第一点的经度坐标</param>longititude1
	 <param name="n2">第二点的纬度坐标</param>latitude2
	 <param name="e2">第二点的经度坐标</param>longititude2
	 */
	public static double Distance(String t1,String t2,String t3,String t4){
		return Distance(Double.parseDouble(t1),Double.parseDouble(t2),Double.parseDouble(t3),Double.parseDouble(t4));
	}
	
	/**
	 * 取方向
	 * @param obj
	 * @return
	 */
	public static String getDirection(Object obj) {
		int index = 0;
		int number = Integer.parseInt(obj.toString());
		int nums[] = new int[] { 0, 45, 90, 135, 180, 225, 270, 315, 360 };
		String array[] = new String[] { "北", "东北", "东", "东南", "南", "西南", "西",
				"西北", "北" };
		int min = 1000;
		for (int i = 0; i < nums.length; i++) {
			int t = Math.abs(number - nums[i]);
			if (t < min) {
				min = t;
				index = i;
			}
		}
		return array[index];
	}

}
